package com.cloud.cop.business.dao;

import javax.transaction.Transaction;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.cloud.cop.CRUDException;
import com.cloud.cop.business.domain.Users;
import com.cloud.cop.business.maps.UsersMapper;

/**
 * <pre>
 * 		1.sqlSessionFactory 应该是整个应用程序有效
 * 		2.session 应该是每个线程有效,且不能共享
 * 		3.entityClass 是操作的对象名称
 * </pre>
 * @author mayh
 *
 */

public class UserDAO<T> {

	private SqlSessionFactory sqlSessionFactory;
	private Class<T> entityClass;
	
	public UserDAO(SqlSessionFactory sqlSessionFactory,Class<T> entityClass){
		this.sqlSessionFactory = sqlSessionFactory;
		this.entityClass = entityClass;
	}
	
	
	public void save() throws CRUDException{
			SqlSession session = null;
			try{
				session = this.sqlSessionFactory.openSession();
				UsersMapper mapper = session.getMapper(UsersMapper.class);
				Users users1 = new Users();
				users1.setName("谷歌1");
				mapper.saveUser(users1);
			}catch(Exception e){
				e.printStackTrace();
				throw new CRUDException();
			}finally {
				session.close();
			}
	}
	
	public void save2() throws CRUDException{
		SqlSession session = null;
		try{
			session = this.sqlSessionFactory.openSession();
			UsersMapper mapper = session.getMapper(UsersMapper.class);
			Users users1 = new Users();
			users1.setName("谷歌2");
			mapper.saveUser(users1);
		    String str = null;
		    System.out.println(str.length());
		}catch(Exception e){
			e.printStackTrace();
			throw new CRUDException();
		}finally {
			session.close();
		}
}
}
